import java.io.*;
import java.util.Scanner;

//进行普通文件的复制
public class HomeWork2 {
    public static void main(String[] args) throws IOException {
        //1.输入对应信息: 这次是文件路径
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要复制的文件路径: ");
        String srcPath = scanner.nextLine();
        //使用next(),遇到空格会停止读取
        System.out.println(srcPath);
        System.out.println("输入要复制到的目标路径: ");
        String destPath = scanner.nextLine();
        System.out.println(destPath);

        //2.判断两个路径是否都是合法的
        File srcFile = new File(srcPath);
        if(!srcFile.isFile()){
            //文件不存在
            System.out.println("要复制的文件路径非法!");
            return;
        }

        File destFile = new File(destPath);//这里要判断这个路径的上级目录是否存在
        if(!destFile.getParentFile().isDirectory()){
            System.out.println("要复制到的目标路径非法!");
            return;
        }

        //3.进行复制
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while (true){
                byte[] bytes = new byte[1024];
                int n = inputStream.read(bytes);
                if(n==-1){
                    break;
                }
                outputStream.write(bytes,0,n);
                outputStream.flush();
            }
        }catch (IOException e){
            throw new IOException(e);
        }
    }
}
